using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using ProgramLauncher.Models;

namespace ProgramLauncher
{
    public partial class ConfigForm : Form
    {
        private AppConfig config;
        private ListView buttonListView;
        private Button addButton;
        private Button editButton;
        private Button deleteButton;
        private Button okButton;
        private Button cancelButton;

        public ConfigForm(AppConfig config)
        {
            this.config = config;

            // 列表视图
            buttonListView = new ListView
            {
                View = View.Details,
                FullRowSelect = true,
                GridLines = true,
                Location = new Point(20, 20),
                Size = new Size(760, 400),
                Font = new Font("Microsoft YaHei UI", 12F)
            };
            buttonListView.Columns.Add("按钮名称", 250);
            buttonListView.Columns.Add("程序路径", 480);
            buttonListView.SelectedIndexChanged += ButtonListView_SelectedIndexChanged;
            Controls.Add(buttonListView);

            // 操作面板
            var operationPanel = new Panel
            {
                Location = new Point(20, 440),
                Size = new Size(760, 70)
            };
            Controls.Add(operationPanel);

            // 添加按钮
            addButton = new Button
            {
                Text = "添加",
                Location = new Point(0, 10),
                Size = new Size(120, 50),
                Font = new Font("Microsoft YaHei UI", 12F),
                BackColor = Color.FromArgb(0, 120, 215),
                ForeColor = Color.White,
                FlatStyle = FlatStyle.Flat,
                Cursor = Cursors.Hand
            };
            addButton.FlatAppearance.BorderSize = 0;
            addButton.Click += AddButton_Click;
            operationPanel.Controls.Add(addButton);

            // 编辑按钮
            editButton = new Button
            {
                Text = "编辑",
                Location = new Point(140, 10),
                Size = new Size(120, 50),
                Font = new Font("Microsoft YaHei UI", 12F),
                BackColor = Color.FromArgb(240, 240, 240),
                FlatStyle = FlatStyle.Flat,
                Enabled = false,
                Cursor = Cursors.Hand
            };
            editButton.FlatAppearance.BorderSize = 1;
            editButton.Click += EditButton_Click;
            operationPanel.Controls.Add(editButton);

            // 删除按钮
            deleteButton = new Button
            {
                Text = "删除",
                Location = new Point(280, 10),
                Size = new Size(120, 50),
                Font = new Font("Microsoft YaHei UI", 12F),
                BackColor = Color.FromArgb(240, 240, 240),
                FlatStyle = FlatStyle.Flat,
                Enabled = false,
                Cursor = Cursors.Hand
            };
            deleteButton.FlatAppearance.BorderSize = 1;
            deleteButton.Click += DeleteButton_Click;
            operationPanel.Controls.Add(deleteButton);

            // 确定按钮
            okButton = new Button
            {
                Text = "确定",
                Location = new Point(520, 10),
                Size = new Size(120, 50),
                Font = new Font("Microsoft YaHei UI", 12F),
                BackColor = Color.FromArgb(0, 120, 215),
                ForeColor = Color.White,
                FlatStyle = FlatStyle.Flat,
                DialogResult = DialogResult.OK,
                Cursor = Cursors.Hand
            };
            okButton.FlatAppearance.BorderSize = 0;
            operationPanel.Controls.Add(okButton);

            // 取消按钮
            cancelButton = new Button
            {
                Text = "取消",
                Location = new Point(640, 10),
                Size = new Size(120, 50),
                Font = new Font("Microsoft YaHei UI", 12F),
                BackColor = Color.FromArgb(240, 240, 240),
                FlatStyle = FlatStyle.Flat,
                DialogResult = DialogResult.Cancel,
                Cursor = Cursors.Hand
            };
            cancelButton.FlatAppearance.BorderSize = 1;
            operationPanel.Controls.Add(cancelButton);

            // 窗体设置
            Text = "按钮配置";
            ClientSize = new Size(800, 530);
            FormBorderStyle = FormBorderStyle.FixedDialog;
            StartPosition = FormStartPosition.CenterParent;
            MaximizeBox = false;
            MinimizeBox = false;
            BackColor = Color.White;

            // 加载按钮列表
            RefreshButtonList();
        }

        private void RefreshButtonList()
        {
            buttonListView.Items.Clear();
            if (config.Buttons != null)
            {
                foreach (var buttonConfig in config.Buttons)
                {
                    var item = new ListViewItem(buttonConfig.Name);
                    item.SubItems.Add(buttonConfig.ProgramPath);
                    item.Tag = buttonConfig;
                    buttonListView.Items.Add(item);
                }
            }
        }

        private void ButtonListView_SelectedIndexChanged(object sender, EventArgs e)
        {
            bool hasSelection = buttonListView.SelectedItems.Count > 0;
            editButton.Enabled = hasSelection;
            deleteButton.Enabled = hasSelection;
        }

        private void AddButton_Click(object sender, EventArgs e)
        {
            using (var buttonConfigForm = new ButtonConfigForm(null))
            {
                if (buttonConfigForm.ShowDialog() == DialogResult.OK)
                {
                    if (config.Buttons == null)
                    {
                        config.Buttons = new List<ButtonConfig>();
                    }
                    config.Buttons.Add(buttonConfigForm.ButtonConfig);
                    RefreshButtonList();
                }
            }
        }

        private void EditButton_Click(object sender, EventArgs e)
        {
            if (buttonListView.SelectedItems.Count > 0)
            {
                var selectedItem = buttonListView.SelectedItems[0];
                var buttonConfig = selectedItem.Tag as ButtonConfig;
                
                using (var buttonConfigForm = new ButtonConfigForm(buttonConfig))
                {
                    if (buttonConfigForm.ShowDialog() == DialogResult.OK)
                    {
                        int index = config.Buttons.IndexOf(buttonConfig);
                        if (index != -1)
                        {
                            config.Buttons[index] = buttonConfigForm.ButtonConfig;
                            RefreshButtonList();
                        }
                    }
                }
            }
        }

        private void DeleteButton_Click(object sender, EventArgs e)
        {
            if (buttonListView.SelectedItems.Count > 0)
            {
                var selectedItem = buttonListView.SelectedItems[0];
                var buttonConfig = selectedItem.Tag as ButtonConfig;
                
                if (MessageBox.Show(
                    $"确定要删除按钮\"{buttonConfig.Name}\"吗？",
                    "确认删除",
                    MessageBoxButtons.YesNo,
                    MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    config.Buttons.Remove(buttonConfig);
                    RefreshButtonList();
                }
            }
        }
    }
} 